<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>评论列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/reset.css">
    <link rel="stylesheet" href="css/iconfont.css">
    <link rel="stylesheet" href="css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <script src="./libs/template-web.js"></script>
    <!-- 导入bootstrap的包 -->
    <script src="./libs/bootstrap/js/bootstrap.min.js"></script>
    <!-- 导入分页插件的包 -->
    <script src="./libs/jquery.twbsPagination.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            评论列表
        </div>
        <div class="container-fluid common_con">
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>作者</th>
                        <th>评论</th>
                        <th>评论在</th>
                        <th>提交于</th>
                        <th>状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>小周</td>
                        <td>这是一条测试评论，欢迎光临</td>
                        <td>《世界，你好》</td>
                        <td>2017-07-04 12:00:00</td>
                        <td>已批准</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr class="danger">
                        <td>小右</td>
                        <td>你好啊，交个朋友好吗？</td>
                        <td>《世界，你好》</td>
                        <td>2017-07-06 14:10:00</td>
                        <td>待审核</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr class="danger">
                        <td>老周</td>
                        <td>不好</td>
                        <td>《世界，你好》</td>
                        <td>2017-07-09 22:22:00</td>
                        <td>待审核</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr class="danger">
                        <td>中周</td>
                        <td>How are you?</td>
                        <td>《世界，你好》</td>
                        <td>2017-07-09 18:22:00</td>
                        <td>待审核</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-info btn-xs">批准</a>
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>小右</td>
                        <td>I am fine thank you and you?</td>
                        <td>《世界，你好》</td>
                        <td>2017-07-11 22:22:00</td>
                        <td>已批准</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>哈哈</td>
                        <td>一针见血</td>
                        <td>《世界，你好》</td>
                        <td>2017-07-22 09:10:00</td>
                        <td>已批准</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-warning btn-xs">拒绝</a>
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>武秀英</td>
                        <td>外影广条同取水权科速工与。矿身面却属次养导务作者用品油调。高石期品极放存斗一号口消别共去。</td>
                        <td>《世界，你好》</td>
                        <td>1970-03-15 11:31:50</td>
                        <td>已拒绝</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>胡娟</td>
                        <td>采参什正面准观提干在易东统。走部系取团商机酸科往证和流物实则。入程用指学行利划影现清关织方。</td>
                        <td>《第四篇示例文章》</td>
                        <td>1970-03-23 14:16:57</td>
                        <td>已拒绝</td>
                        <td class="text-center">
                            <a href="javascript:void(0);;" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>邵艳</td>
                        <td>国新研目心思力品家织通还如周气长多。话它思造约众系压程它过去全。必导则达发前何西最老四关向。</td>
                        <td>《第一篇示例文章》</td>
                        <td>1970-04-19 12:34:27</td>
                        <td>已拒绝</td>
                        <td class="text-center">
                            <a href="javascript:editTr( 'trashed',10 );" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                    <tr>
                        <td>唐军</td>
                        <td>好联律物联使进很们有严这里月之。实养件矿商除政究定划必火起划六。内百那则变次引持只情车各地织持。</td>
                        <td>《第四篇示例文章》</td>
                        <td>1970-04-24 11:22:29</td>
                        <td>已批准</td>
                        <td class="text-center">
                            <a href="javascript:editTr( 'rejected',11 );" class="btn btn-warning btn-xs">拒绝</a>
                            <a href="javascript:editTr( 'trashed',11 );" class="btn btn-danger btn-xs">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center">
                <!-- 分页导航的插件容器 -->
                <ul id="pagination" class="pagination-sm"></ul>
            </div>

        </div>
    </div>
</body>

</html>
<!-- 渲染评论列表的模板 -->
<script type="text/html" id="listtem">
    {{each data.data v}} {{if v.state=='待审核'}}
    <tr class="danger">
        {{else}}
        <tr>
            {{/if}}
            <td>{{v.author}}</td>
            <td>{{v.content}}</td>
            <td>{{v.title}}</td>
            <td>{{v.date}}-{{v.time}}</td>
            <td>{{v.state}}</td>
            <td class="text-center">
                {{if v.state=='待审核'}}
                <a href="javascript:void(0);;" class="btn btn-info btn-xs btn-yes" data-id="{{v.id}}">批准</a> {{else if v.state=='已通过'}}

                <a href="javascript:void(0);;" class="btn btn-warning btn-xs btn-no" data-id="{{v.id}}">拒绝</a> {{/if }}

                <a href="javascript:void(0);;" class="btn btn-danger btn-xs btn-del" data-id="{{v.id}}">删除</a>
            </td>
        </tr>
        {{/each}}
</script>


<script src="./libs/https.js"></script>
<script src="./"></script>
<script>
    $(function() {

        //声明一个变量,用于保存已经选中并点击的页码
        let selPage = 1;

        //因为之后,有很多地方需要用到这个获取评论列表的地方,所以封装一个函数先
        function getComents(currentpage, callback) {
            $.ajax({
                url: BigNew.comment_list,
                type: 'get',
                data: {
                    page: currentpage,
                    perpage: 10
                },
                success: function(backData) {
                    console.log(backData);
                    if (backData.code == 200) {
                        let listStr = template('listtem', backData);
                        $('tbody').html(listStr);

                        //判断是否有回调函数
                        if (callback != null && backData.data.data.length != 0) {
                            //当回调函数不为null 的时候,并且,当前页面有数据的时候,就调用回调函数
                            callback(backData);
                        } else if (backData.data.totalPage == selPage - 1 && backData.data.data.length == 0) {
                            //当数据的总页码数等于当前所在的页码数-1的时并且当前的页码中的数据为空的时候                          
                            //所以需要重绘分页插件,默认跳转到当前页面的前一页
                            $('#pagination').twbsPagination('changeTotalPages', backData.data.totalPage, selPage - 1);
                        }

                    }
                }
            });
        }
        //渲染完页面后,然后回调函数执行了分页插件
        getComents(selPage, function(backDataXC) {
            $('#pagination').twbsPagination({
                first: '首页',
                last: '尾页',
                prev: '上一页',
                next: '下一页',
                totalPages: backDataXC.data.totalPage, //总页数,不能写死
                visiblePages: 10, //一个ul中显示几个li

                //实现根据点击分页码来再次渲染列表并跳转对应的页码信息
                onPageClick: function(event, page) {
                    selPage = page;
                    //点击了页码后,还是在这个分类中,所以'分页表的总页数'并没有改变,,所以不用重新绘制分页插件列表
                    //再次请求渲染页面列表结构-----这次调用方法时,仅仅只是为了渲染页面,渲染之后,不做任何的操作,所以第二个参数回调函数为null
                    getComents(selPage, null)
                }
            });
        });


        //1.点击批准按钮
        $('tbody').on('click', '.btn-yes', function(e) {
            e.preventDefault();
            let wzId = $(this).attr('data-id');
            $.ajax({
                url: BigNew.comment_pass,
                type: 'post',
                data: {
                    id: wzId
                },
                success: function(backData) {
                    console.log(backData);
                    if (backData.code == 200) {
                        alert('以批准');
                        getComents(selPage, null);
                    }
                }
            });
        })

        //2.点击拒绝按钮
        $('tbody').on('click', '.btn-no', function(e) {
            e.preventDefault();
            let wzId = $(this).attr('data-id');
            $.ajax({
                url: BigNew.comment_reject,
                type: 'post',
                data: {
                    id: wzId
                },
                success: function(backData) {
                    console.log(backData);
                    if (backData.code == 200) {
                        alert('已拒绝');
                        getComents(selPage, null);
                    }
                }
            });
        })

        //3.点击删除按钮
        $('tbody').on('click', '.btn-del', function(e) {
            e.preventDefault();
            let wzId = $(this).attr('data-id');
            $.ajax({
                url: BigNew.comment_delete,
                type: 'post',
                data: {
                    id: wzId
                },
                success: function(backData) {
                    console.log(backData);
                    if (backData.code == 200) {
                        alert('已删除');
                        getComents(selPage, function(backDataXC) {
                            //点击了删除后,因为删除后很多个之后,,会影响总数据数量,所以分页插件也会根据数据总数量进行变化,所以需要重绘分页插件
                            $('#pagination').twbsPagination('changeTotalPages', backDataXC.data.totalPage, selPage);
                        });
                    }
                }
            });
        })



    })
</script>